package com.fadis.protocol;

import com.fadis.FadisConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 空array：*0\r\n
 * null array： *-1\r\n
 * array元素可以：
 * -- 可以是任何FakeRespCmd子类
 * -- 可以是null -- 空批量回复null
 */
public class RespArray extends BaseRespProtocol {

    private List<BaseRespProtocol> list = new ArrayList<>();

    public RespArray() {
    }

    public void add(BaseRespProtocol cmd){
        if(list == null) list = new ArrayList<>();
        list.add(cmd);
    }

    public void makeNull(){
        list = null;
    }

    public boolean isNull(){
        return list == null;
    }

    public int getCount() {
        return list == null ? 0 : list.size();
    }

    public BaseRespProtocol get(int index){
        if(list == null || index < 0 || index >= list.size()) return null;
        return list.get(index);
    }

    @Override
    public String encode() {
        if(list == null) return "*-1\r\n";
        if(list.size() == 0) return "*0\r\n";
        StringBuilder sb = new StringBuilder();

        sb.append("*" + list.size() + FadisConstant.CR + FadisConstant.LF);
        for (int i = 0; i < list.size(); i++) {
            BaseRespProtocol arg = list.get(i);
            sb.append(arg.encode());
        }

        return sb.toString();
    }

    public static RespArray from(List<String> list){
        RespArray a = new RespArray();
        if(list == null){
            a.makeNull();
        }else{
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i) == null){
                    a.add(new BulkString());
                }else{
                    a.add(new BulkString(list.get(i)));
                }
            }
        }

        return a;
    }

    public static RespArray from(Set<String> list){
        RespArray a = new RespArray();
        if(list == null){
            a.makeNull();
        }else{
            for (String s: list) {
                a.add(new BulkString(s));
            }
        }

        return a;
    }

    public static RespArray fromCommon(List list){
        RespArray a = new RespArray();
        if(list == null){
            a.makeNull();
        }else{
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i) == null){
                    a.add(new BulkString());
                }else{
                    Object obj = list.get(i);
                    if(obj instanceof Integer){
                        a.add(new LongNumber((Integer)obj));
                    }else if(obj instanceof Long){
                        a.add(new LongNumber((Long)obj));
                    }else if(obj instanceof String){
                        a.add(new BulkString((String) obj));
                    }

                }
            }
        }

        return a;
    }


    public static String encodeCommon(List list){
        return CmdEncoder.getDefault().encode(fromCommon(list));
    }

    public static String encodeList(List<String> list){
        return CmdEncoder.getDefault().encode(from(list));
    }

    public static String encodeSet(Set<String> set){
        return CmdEncoder.getDefault().encode(from(set));
    }
}
